const {getDmg,reduceResistanceK,lvSuppressK,objValueAdd } = require("../libs/apis.js")

const WEAPONS = require("./weapons.js");

const CLIPDATA_TYPE = {
    max:{
        fixedhp: 298.75,
        fixedAtt: 19.45,
        fixdefence: 23.15,
        reaction : 23.31,
        attRate: 5.83, 
        hpRate: 5.83, 
        criticalRate: 3.89,
        criticalStrike: 7.77,
        defRate: 7.29,
        charging: 6.48
    },
    mid:{
        fixedhp: 253.94,   // 209.13  239   268.88   298.75
        fixdefence: 19.675, // 16.2   18.52  20.83  23.15
        fixedAtt: 16.535,   //  13.62   15.56   17.51  19.45
        reaction : 19.815,  // 16.32   18.65  20.98  23.31
        attRate: 4.955,  // 4.08  4.66  5.25  5.83
        hpRate: 4.955,   // 4.08  4.66  5.25  5.83
        criticalRate: 3.305,   //  2.72  3.11   3.5   3.89
        criticalStrike: 6.605,  //  5.44  6.22  6.99  7.77
        defRate: 6.195,   //  5.1  5.83  6.56   7.29
        charging: 5.505  //  4.53   5.18   5.83   6.48
    }
}


const CLIPDATA = CLIPDATA_TYPE.mid


const CONFIG = {
    chance: 20,
    your_lv: 90,
    enemy_lv: 90,
    clips_main: {
        criticalRate: 31.1,
        dmgRate: 46.6,
        hpRate: 46.6,
    },
    clips: ["hpRate", "reaction", "criticalRate", "criticalStrike"],
    clips_initial: [
        ["reaction", "hpRate", "criticalRate", "criticalStrike"],
        ["reaction", "hpRate", "criticalRate", "criticalStrike"],
        ["charging", "reaction", "criticalRate", "criticalStrike"],
        ["reaction", "hpRate", "criticalRate", "criticalStrike"],
        ["hpRate", "reaction", "charging", "criticalStrike"]
    ],
    reactionK : 2,
    buffs: [  
        { dmgRate: 15, jk: 30}, // 
    ]
}

let charaters = {
    units : {
        name: "白术", 
        charging: 100,
        defence: 499.56,
        attRate:0,
        hpRate:28.8,
        hp:13348,
        base: 192.54,    
        criticalRate: 5,    
        criticalStrike: 50,
        skills: {
            a: [
                function(data){},
            ]
        }
    },
}
 
let cons = 0;
let selIndex= 0;
/*
    e: 驱役游丝徵灵，使它在附近的敌人间游弋攻击，造成草元素伤害。在进行至多3次攻击后，
    或是在附近不存在敌人的情况下，游丝徵灵将会返回，为队伍中附近的所有角色恢复生命值，回复量受益于白术的生命值上限
    lv 10 倍率 142.56% 治疗 17.28% + 2033.95
    lv 13 倍率 168.3% 治疗 20.4% + 2542.46
    q: 进入脉摄宣明状态，产生无郤气护盾。 在脉摄宣明状态下，每2.5秒将产生新的无郤气护盾。
    无郤气护盾在以下情况下，将会为当前场上自己的角色恢复生命值，回复量受益于白术的生命值上限，并释放灵气脉攻击敌人，造成草元素伤害：
    ·当角色处于无郤气护盾的庇护下时，并产生新的无郤气护盾；
    ·无郤气护盾的效果结束，或因伤害破碎。  
    大致认为2.5s触发一次
    持续14秒 80能量 20秒cd   
    lv 10  盾 1.44%+169.5 治疗 9.36%+1101.72 伤害145.73%
    lv 13  盾 1.7%+211.87 治疗 11.05%+1377.17 伤害172.04%

    天赋1 与部分采集物交互回血 2.5%
    天赋2 场上角色血低于一半时 白术获得20%治疗加成 高于一半时 白术获得25%草伤
    天赋3 受到白术大招盾下的角色 基于白术生命上限不超过5w的部分 每1000点给该角色的 燃烧绽放增伤2 激化增伤0.8 
    也就是5w血 100燃烧绽放增伤  40激化增伤

    命座
    e使用次数+1
    场上角色攻击命中敌人时 协同攻击 300%倍率 以及 e的20%治疗量 cd 6s
    q+3
    放q后全队精通+80
    e+3
    愈气全形论的灵气脉造成的伤害提高，提升值相当于白术生命值上限的6%。
    此外，游丝徵灵或游丝徵灵·切在命中敌人时，有100%概率产生愈气全形论的无郤气护盾，该效果每个游丝徵灵或游丝徵灵·切只能触发一次。
 */

calc(calc_clips_initial());

function calc(normal) {
    let charactersDmg = [];
    let count = 0;
    Object.values(WEAPONS).forEach(weapon => {
        if(weapon.weapontype == "magic") {
            charactersDmg.push(getDMG([charaters.units,weapon,{fixedAtt:311,...CONFIG.clips_main},normal], (data,second) => {
                count++;
                data = objValueAdd([data, ...CONFIG.buffs]);
                data.hp = data.hp*(1 + (data.hpRate||0)/100 + 0.18 + 0) + 4780;
                data.dmgRate = (data.dmgRate||0) + 25
                if(weapon.skill) { weapon.skill(data); }
                
                // slipQadd = 0 //  
                let rea = { reaction:data.reaction, reactionQ:CONFIG.reactionK, reactionDmgAdd:data.reactionDmgAdd || 0 }
    
                let jhDmgZhi = 1447 * 1.25 * (1 + (5*data.reaction)/(data.reaction+1200))

                let eDmg = getDmg({
                    data: {
                        ...data,
                        dmgRate: data.dmgRate + (data.eDmgRate||0) + (data.eDmgAdd||0),
                    },
                    rate: cons >= 5 ? 1.683: 1.4256
                }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
                let eDmgJH = getDmg({
                    data: {
                        ...data,
                        dmgRate: data.dmgRate + (data.eDmgRate||0) + (data.eDmgAdd||0),
                        dmgZhi: jhDmgZhi
                    },
                    rate: cons >= 5 ? 1.683: 1.4256
                }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
                let eNai = data.hp * (cons >= 5 ? 0.204: 0.1728) + (cons >= 5 ? 2542.46: 2033.95)

                // lv 10  盾 1.44%+169.5 治疗 9.36%+1101.72 伤害145.73%
                // lv 13  盾 1.7%+211.87 治疗 11.05%+1377.17 伤害172.04%

                let qDun = data.hp * (cons >= 3 ?  0.017 : 0.0144) + (cons >= 3 ? 211.87: 169.5)
                let qNai = data.hp * (cons >= 3 ? 0.1105: 0.0936) + (cons >= 3 ? 1377.17: 1101.72)
                let qDmg = getDmg({
                    data: {
                        ...data,
                        dmgRate: data.dmgRate,
                    },
                    rate: cons >= 3 ? 1.7204: 1.4573
                }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
                let qDmgJH = getDmg({
                    data: {
                        ...data,
                        dmgRate: data.dmgRate,
                        dmgZhi: jhDmgZhi
                    },
                    rate: cons >= 3 ? 1.7204: 1.4573
                }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
    
                // let point = (eDmg * 2 + eDmgJH) * (cons >= 3? 3:2) + (qDmg * 2 + qDmgJH * 3)
                let point = eNai * (cons >= 3? 3:2) + qNai*5
                return {point, data, second, dmg:{
                    e伤害:eDmg,e激化伤害:eDmgJH,e奶量:eNai,q盾量:qDun,q奶量:qNai,q伤害:qDmg,q激化伤害:qDmgJH
                }}
            }));
        }
    })
    charactersDmg.sort((a,b)=>{
        return (b.point)-(a.point);
    });
    console.log(charactersDmg[selIndex]);
    console.log(charactersDmg.map((v, n)=>{
        return  `(${n})得分: ${v.data.weaponname} : ${v.point.toFixed(5)}`
    }));
    console.log(count);
}

function getDMG(dataarr, getRet){
    let chance = CONFIG.chance;
    let _ret = [];
    for (let index = 0; index <= chance; index++) {
        let c1 = index; 
        for (let index2 = 0; index2 <= chance - index; index2++) {
            let c2 = index2;
            for (let index3 = 0; index3 <= chance-index-index2; index3++) {
                let c3 = index3;
                let c4 = chance - index - index2 - index3;
                let cs = [c1, c2, c3, c4];
                var tmpdata = {};
                CONFIG.clips.forEach((v, i) => {
                    tmpdata[v] = CLIPDATA[v] * cs[i]
                })
                _ret.push(tmpdata);
            }
        }
    }
    _ret = _ret.map(second => {
        let panel = objValueAdd([...dataarr, second]);
        return  getRet(panel, second)
    })
    _ret.sort((b,a)=> a.point-b.point)
    return _ret[0]
}

// 固定攻击力 19  防御力23 生命值299 5.3% 
function calc_clips_initial () {
    let clips = CONFIG.clips_initial.map(v => {
        let tmp = {};
        v.forEach(v2 => {
            tmp[v2] = CLIPDATA[v2]
        })
        return tmp
    })
    let data = {}
    clips.forEach(item => {
        for (const key in item) {
            data[key] = data[key]? (data[key]+item[key]) : item[key]
        }
    })
    return data
}
